Bitwise and bitwise and or | bitwise hetero or ^ operators in C++

  • 2020-05-07 20:11:59
  • OfStack

bitwise and operator: &

grammar


expression 
&
 expression

note
Expressions can be other "and" expressions, or (subject to the type restrictions described below) equal expressions, relational expressions, addition expressions, multiplication expressions, pointer expressions to members, cast expressions, 1 yuan expressions, postfix expressions, or main expressions.
The bitwise and operator (&) compares each bit of the first operand with the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, set the corresponding result bit to 0.
The two operands of the bitwise and operator must be integer. The usual arithmetic transformations described in arithmetic transformations are applied to operands.
The operator keyword of &
The bitand operator is the text equivalent of &. There are two ways to access the bitand operator in a program: include the header file iso646.h, or compile with the /Za (language extension disabled) compiler option.


// expre_Bitwise_AND_Operator.cpp
// compile with: /EHsc
// Demonstrate bitwise AND
#include <iostream>
using namespace std;
int main() {
  unsigned short a = 0xFFFF;   // pattern 1111 ...
  unsigned short b = 0xAAAA;   // pattern 1010 ...

  cout << hex << ( a & b ) << endl;  // prints "aaaa", pattern 1010 ...
}

bitwise and or operator: |

grammar


expression 
|
 expression

note
The bitwise and or operator (|) compares each bit of the first operand with the corresponding bit of the second operand. If one of the bits is 1, the corresponding result bit is set to 1. Otherwise, set the corresponding result bit to 0.
The two operands of the bitwise and or operator must be integer. The commonly used arithmetic transformations covered in arithmetic transformations apply to operands.
Operator keyword for |
The bitor operator is the text equivalent of |. The bitor operator in the accessor can be compiled in two ways: by including the header file iso646.h, or by using the /Za (language extension disabled) compiler option.


// expre_Bitwise_Inclusive_OR_Operator.cpp
// compile with: /EHsc
// Demonstrate bitwise inclusive OR
#include <iostream>
using namespace std;

int main() {
  unsigned short a = 0x5555;   // pattern 0101 ...
  unsigned short b = 0xAAAA;   // pattern 1010 ...

  cout << hex << ( a | b ) << endl;  // prints "ffff" pattern 1111 ...
}

bitwise xor operator: ^

grammar


expression ^ expression

note
The bitwise xor operator (^) compares each bit of the first operand with the corresponding bit of the second operand. If one bit is 0 and the other bit is 1, the corresponding result bit is set to 1. Otherwise, set the corresponding result bit to 0.
Both operands of the bitwise xor operator must be integral. The commonly used arithmetic transformations covered in arithmetic transformations apply to operands.
Operator keyword for ^
The xor operator is the text equivalent of ^. In your program, the xor operator can be accessed in two ways: by including the header file iso646.h, or by compiling with the /Za (language extension disabled) compiler option.

// expre_Bitwise_Exclusive_OR_Operator.cpp
// compile with: /EHsc
// Demonstrate bitwise exclusive OR
#include <iostream>
using namespace std;
int main() {
  unsigned short a = 0x5555;   // pattern 0101 ...
  unsigned short b = 0xFFFF;   // pattern 1111 ...
  cout << hex << ( a ^ b ) << endl;  // prints "aaaa" pattern 1010 ...
}

Related articles: